package com.dating.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dating.entities.ArrayValues;
import com.dating.entities.AsyntaskDating;
import com.dating.entities.Me;
import com.dating.libs.client.parttern.doit.OnTaskListener;
import com.dating.libs.system.lazyload.LazyAdapter;
import com.dating.libs.web.services.datingservice.DatingService;
import com.dating.libs.web.services.datingservice.UserProperties;
import com.dating.pref.Constant;
import com.dating.pref.Pref;

public class Dating extends Activity {
	public static final String TAG = "Dating";

	// VARIABLE
	public static Me ME;
	static Toast t;
	static Handler mHandler;
	boolean cancel = false;

	// VIEW
	ListView mList;
	LazyAdapter mAdapter;

	AsyntaskDating mAsyn;

	// LISTENER
	OnTaskListener mInfoMeListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dating);

		mList = (ListView) findViewById(R.id.list_image);
		mAdapter = new LazyAdapter(this, ArrayValues.URL_IMAGE);

		// mList.setAdapter(mAdapter);
		// clear cache
		mAdapter.imageLoader.clearCache();

		t = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		mHandler = new Handler();
		mAsyn = new AsyntaskDating();

		initListener();

		mAsyn.setOnTaskListener(mInfoMeListener);

		// get user info
		mAsyn.execute(ME.UID);
		startActivity(new Intent(this, ArroundMap.class));
	}

	private void initListener() {
		mInfoMeListener = new OnTaskListener() {
			DatingService dating = new DatingService();

			@Override
			public void onPrepare(String... values) {
				Dating.this.showDialog(Constant.DIALOG_LOADING);
			}

			@Override
			public void onDoBackground(String... params) {
				try {
					if (!ME._latitude.equals("") && !ME._longitude.equals("0")) {
						try {
							dating.updateMyPosition(
									Float.parseFloat(ME._latitude),
									Float.parseFloat(ME._longitude));
							showToast("update position success");
						} catch (Exception e) {
							mHandler.post(new Runnable() {

								@Override
								public void run() {
									showToast("update position failed");
								}
							});
						}
					}
					ArrayList<UserProperties> _t = dating
							.getAllUserProperties(Integer.parseInt(ME.UID));
					if (_t.size() > 0) {
						ME.mMeProperties = _t;
					}
					for (UserProperties _i : _t) {
						Log.e(_i.getName(""), _i.getValue(""));
					}
				} catch (NullPointerException e) {
					showToast("Not user info");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onComplete(Boolean values) {
				Dating.this.dismissDialog(Constant.DIALOG_LOADING);
				if (values)
					updateUI();
			}
		};
	}

	public void updateUI() {
		if (ME.mMeProperties != null && ME.mMeProperties.size() > 0) {
			TextView t = (TextView) findViewById(R.id.text);
			StringBuilder _str = new StringBuilder();
			for (UserProperties _i : ME.mMeProperties) {
				_str.append(_i.getName("") + _i.getValue(""));
			}
			// Log.e("user info", _str.toString());
			t.setText(_str.toString());
		}
	}

	@Deprecated
	public void getUserProperties(int userId, String token) {
		showDialog(Constant.DIALOG_LOADING);
		DatingService dt = new DatingService();
		try {
			UserProperties _t = dt.getUserProperties(userId, token);
			showToast("pro:" + _t.getName());
		} catch (NullPointerException e) {
			showToast("Cannot get properties");
		} catch (Exception e) {
			e.printStackTrace();
		}
		dismissDialog(Constant.DIALOG_LOADING);
	}

	public void btnClicked(View v) {
		switch (v.getId()) {
		case R.id.btnPhoto:
			startActivity(new Intent(this, UserInfo.class));
			break;
		case R.id.btnMatching:
			startActivity(new Intent(this, Matching.class));
			break;
		case R.id.btnChat:
			startActivity(new Intent(this, Chat.class));
			break;
		case R.id.btnEditInfo:
			startActivity(new Intent(this, EditInfo.class));
			break;
		case R.id.btnSuggest:
			startActivity(new Intent(this, Suggest.class));
			break;
		case R.id.btnMap:
			startActivity(new Intent(this, ArroundMap.class));
			break;
		case R.id.btnShowImg:
			Intent i=new Intent(this, ShowImage.class);
			Bundle b=new Bundle();
			b.putString("user_id", "82");
			i.putExtras(b);
			startActivity(i);
			break;
		case R.id.btnPref:
			startActivity(new Intent(this, Pref.class));
			break;
		default:
			break;
		}

	}

	// SUPER METHOD
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		Dialog d = new Dialog(this);
		d.setCancelable(true);
		// d.setCanceledOnTouchOutside(true);
		d.requestWindowFeature(Window.FEATURE_NO_TITLE);
		d.setContentView(R.layout.loading);
		switch (id) {
		case Constant.DIALOG_LOADING:
			d.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					cancel = true;
				}
			});
			// d.setTitle("Loging in...");
			cancel = false;
			d.show();
			break;

		default:
			break;
		}
		return d;
	}

	public static void showToast(String text) {
		if (t != null) {
			t.cancel();
		}
		t.setText(text);
		t.show();
	}

	@Override
	protected void onDestroy() {
		mList.setAdapter(null);
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		if (ME == null) {
			ME = Me.getInstance(ME);
		}
		super.onResume();
	}
}
